package com.ujs.code.zqh;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GoodSubstrings {
    public static void main(String[] args) {
        String input = "10010"; // 替换为你的字符串
        int goodSubstringCount = countGoodSubstrings(input);
        System.out.println("好子串的数量为: " + goodSubstringCount);
    }

    public static int countGoodSubstrings(String s) {
        int goodSubstringCount = 0;
        for (int i = 0; i < s.length(); i++) {
            for (int j = i; j <= s.length(); j++) {
                String currentChar = s.substring(i, j);
                if (currentChar.startsWith("0")) {
                    int zeroCount = 0; // 0的数量
                    int oneCount = 0;  // 1的数量
                    if ("0".equals(currentChar)) {
                        goodSubstringCount++;
                    } else {
                        Boolean flag = false;
                        for (int k = 0; k < currentChar.length(); k++) {
                            if (currentChar.charAt(k) == '1') {
                                flag = true;
                                zeroCount = k;
                                oneCount = countOnes(currentChar);
                            }
                        }
                        if (!flag) {
                            goodSubstringCount++;
                        }
                        // 检查是否满足好子串的条件
                        if (zeroCount > oneCount) {
                            goodSubstringCount++;
                        }
                    }

                }
            }
        }

        return goodSubstringCount;
    }

    public static int countOnes(String input) {
        int onesCount = 0;
        for (int i = 0; i < input.length(); i++) {
            char currentChar = input.charAt(i);
            if (currentChar == '1') {
                onesCount++;
            }
        }
        return onesCount;
    }

}
